﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace Infrastructure.Swagger
{

    public class PublicHeaderParameter : IOperationFilter
    {

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null) operation.Parameters = new List<OpenApiParameter>();
            var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo;

            //先判断是否是匿名访问,
            var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
            if (descriptor != null)
            {
                var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true);
                bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
                //非匿名的方法,链接中添加accesstoken值
                //if (!isAnonymous)
                {
                  
                    operation.Parameters.Add(new OpenApiParameter()
                    {
                        Name = "platform",
                        In = ParameterLocation.Header,//query header body path formData
                        //Type = "string",
                        Required = false, //是否必选
                        Description = "0 为IOS	1 Android",
                        //Default = "0"
                    });

                    operation.Parameters.Add(new OpenApiParameter()
                    {
                        Name = "version",
                        In = ParameterLocation.Header,//query header body path formData
                        //Type = "string",
                        Required = false, //是否必选
                        Description = "int类型   Android和IOS 定义一个Int值 每发一个版本二个值增加1 版本相同就保持一致",
                        //Default = "1"
                    });

                    operation.Parameters.Add(new OpenApiParameter()
                    {
                        Name = "DeviceID",
                        In = ParameterLocation.Header,//query header body path formData
                        //Type = "string",
                        Required = false, //是否必选
                        Description = "设备唯一ID",
                        //Default = "0"
                    });
                    operation.Parameters.Add(new OpenApiParameter()
                    {
                        Name = "vToken",
                        In = ParameterLocation.Header,//query header body path formData
                        //Type = "string",
                        Required = false, //是否必选
                        Description = "Post接口需要的验证Token   登录前通过 /api/Token/GetDeviceToken获取  登录后通过 /api/Token/GetUserToken获取",
                        //Default = "0"
                    });
                }
            }
        }
    }

}
